;
;           Copyright Oliver Kowalke 2009.
;           Copyright Thomas Sailer 2013.
;  Distributed under the Boost Software License, Version 1.0.
;     (See accompanying file LICENSE_1_0.txt or copy at
;           http://www.boost.org/LICENSE_1_0.txt)


;************************************************************************************
; ---------------------------------------------------------------------------------- *
; |     0   |     1   |     2    |     3   |     4   |     5   |     6   |     7   | *
; ---------------------------------------------------------------------------------- *
; |    0x0  |    0x4  |    0x8   |    0xc  |   0x10  |   0x14  |   0x18  |   0x1c  | *
; ---------------------------------------------------------------------------------- *
; |                          SEE registers (XMM6-XMM15)                            | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |     8   |    9    |    10    |    11   |    12   |    13   |    14   |    15   | *
; ---------------------------------------------------------------------------------- *
; |   0x20  |  0x24   |   0x28   |   0x2c  |   0x30  |   0x34  |   0x38  |   0x3c  | *
; ---------------------------------------------------------------------------------- *
; |                          SEE registers (XMM6-XMM15)                            | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    16   |    17   |    18   |    19    |    20   |    21   |    22   |    23   | *
; ---------------------------------------------------------------------------------- *
; |   0xe40  |   0x44 |   0x48  |   0x4c   |   0x50  |   0x54  |   0x58  |   0x5c  | *
; ---------------------------------------------------------------------------------- *
; |                          SEE registers (XMM6-XMM15)                            | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    24   |   25    |    26    |   27    |    28   |    29   |    30   |    31   | *
; ---------------------------------------------------------------------------------- *
; |   0x60  |   0x64  |   0x68   |   0x6c  |   0x70  |   0x74  |   0x78  |   0x7c  | *
; ---------------------------------------------------------------------------------- *
; |                          SEE registers (XMM6-XMM15)                            | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    32   |   32    |    33    |   34    |    35   |    36   |    37   |    38   | *
; ---------------------------------------------------------------------------------- *
; |   0x80  |   0x84  |   0x88   |   0x8c  |   0x90  |   0x94  |   0x98  |   0x9c  | *
; ---------------------------------------------------------------------------------- *
; |                          SEE registers (XMM6-XMM15)                            | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    39   |   40    |    41    |   42    |    43   |    44   |    45   |    46   | *
; ---------------------------------------------------------------------------------- *
; |   0xa0  |   0xa4  |   0xa8   |   0xac  |   0xb0  |   0xb4  |   0xb8  |   0xbc  | *
; ---------------------------------------------------------------------------------- *
; | fc_mxcsr|fc_x87_cw|     <alignment>    |       fbr_strg    |      fc_dealloc   | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    47   |   48    |    49    |   50    |    51   |    52   |    53   |    54   | *
; ---------------------------------------------------------------------------------- *
; |   0xc0  |   0xc4  |   0xc8   |   0xcc  |   0xd0  |   0xd4  |   0xd8  |   0xdc  | *
; ---------------------------------------------------------------------------------- *
; |        limit      |         base       |         R12       |         R13       | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    55   |   56    |    57    |   58    |    59   |    60   |    61   |    62   | *
; ---------------------------------------------------------------------------------- *
; |   0xe0  |   0xe4  |   0xe8   |   0xec  |   0xf0  |   0xf4  |   0xf8  |   0xfc  | *
; ---------------------------------------------------------------------------------- *
; |        R14        |         R15        |         RDI       |        RSI        | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    63   |   64    |    65    |   66    |    67   |    68   |    69   |    70   | *
; ---------------------------------------------------------------------------------- *
; |  0x100  |  0x104  |  0x108   |  0x10c  |  0x110  |  0x114  |  0x118  |  0x11c  | *
; ---------------------------------------------------------------------------------- *
; |        RBX        |         RBP        |       hidden      |        RIP        | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    71   |   72    |    73    |   74    |    75   |    76   |    77   |    78   | *
; ---------------------------------------------------------------------------------- *
; |  0x120  |  0x124  |  0x128   |  0x12c  |  0x130  |  0x134  |  0x138  |  0x13c  | *
; ---------------------------------------------------------------------------------- *
; |                                   parameter area                               | *
; ---------------------------------------------------------------------------------- *
; ---------------------------------------------------------------------------------- *
; |    79   |   80    |    81    |   82    |    83   |    84   |    85   |    86   | *
; ---------------------------------------------------------------------------------- *
; |  0x140  |  0x144  |  0x148   |  0x14c  |  0x150  |  0x154  |  0x158  |  0x15c  | *
; ---------------------------------------------------------------------------------- *
; |       FCTX        |        DATA        |                                       | *
; ---------------------------------------------------------------------------------- *
;************************************************************************************

PUBLIC jump_fcontext
.CODE
ALIGN 16
jump_fcontext PROC

    lea         rsp,[rsp-118h] ; prepare stack

IFNDEF BOOST_USE_TSX
; save XMM storage
    movaps      xmmword ptr [rsp],xmm6
    movaps      xmmword ptr [rsp+10h],xmm7
    movaps      xmmword ptr [rsp+20h],xmm8
    movaps      xmmword ptr [rsp+30h],xmm9
    movaps      xmmword ptr [rsp+40h],xmm10
    movaps      xmmword ptr [rsp+50h],xmm11
    movaps      xmmword ptr [rsp+60h],xmm12
    movaps      xmmword ptr [rsp+70h],xmm13
    movaps      xmmword ptr [rsp+80h],xmm14
    movaps      xmmword ptr [rsp+90h],xmm15
    stmxcsr     dword ptr [rsp+0A0h]  ; save MMX control- and status-word
    fnstcw      word  ptr [rsp+0A4h]  ; save x87 control-word
ENDIF

; load NT_TIB
    mov         r10,qword ptr gs:[30h]
; save fiber local storage
    mov         rax,qword ptr [r10+20h]
    mov         qword ptr [rsp+0B0h],rax
; save current deallocation stack
    mov         rax,qword ptr [r10+1478h]
    mov         qword ptr [rsp+0B8h],rax
; save current stack limit
    mov         rax,qword ptr [r10+10h]
    mov         qword ptr [rsp+0C0h],rax
; save current stack base
    mov         rax,qword ptr [r10+8]
    mov         qword ptr [rsp+0C8h],rax

    mov         qword ptr [rsp+0D0h],r12  ; save R12
    mov         qword ptr [rsp+0D8h],r13  ; save R13
    mov         qword ptr [rsp+0E0h],r14  ; save R14
    mov         qword ptr [rsp+0E8h],r15  ; save R15
    mov         qword ptr [rsp+0F0h],rdi  ; save RDI
    mov         qword ptr [rsp+0F8h],rsi  ; save RSI
    mov         qword ptr [rsp+100h],rbx  ; save RBX
    mov         qword ptr [rsp+108h],rbp  ; save RBP

    mov         qword ptr [rsp+110h],rcx  ; save hidden address of transport_t

; preserve RSP (pointing to context-data) in R9
    mov         r9,rsp

; restore RSP (pointing to context-data) from RDX
    mov         rsp,rdx

IFNDEF BOOST_USE_TSX
; restore XMM storage
    movaps      xmm6,xmmword ptr [rsp]
    movaps      xmm7,xmmword ptr [rsp+10h]
    movaps      xmm8,xmmword ptr [rsp+20h]
    movaps      xmm9,xmmword ptr [rsp+30h]
    movaps      xmm10,xmmword ptr [rsp+40h]
    movaps      xmm11,xmmword ptr [rsp+50h]
    movaps      xmm12,xmmword ptr [rsp+60h]
    movaps      xmm13,xmmword ptr [rsp+70h]
    movaps      xmm14,xmmword ptr [rsp+80h]
    movaps      xmm15,xmmword ptr [rsp+90h]
    ldmxcsr     dword ptr [rsp+0A0h] ; restore MMX control- and status-word
    fldcw       word  ptr [rsp+0A4h] ; restore x87 control-word
ENDIF
; load NT_TIB
    mov         r10,qword ptr gs:[30h]
; restore fiber local storage
    mov         rax,qword ptr [rsp+0B0h]
    mov         qword ptr [r10+20h],rax
; restore current deallocation stack
    mov         rax,qword ptr [rsp+0B8h]
    mov         qword ptr [r10+1478h],rax
; restore current stack limit
    mov         rax,qword ptr [rsp+0C0h]
    mov         qword ptr [r10+10h],rax
; restore current stack base
    mov         rax,qword ptr [rsp+0C8h]
    mov         qword ptr [r10+8],rax

    mov         r12,qword ptr [rsp+0D0h]  ; restore R12
    mov         r13,qword ptr [rsp+0D8h]  ; restore R13
    mov         r14,qword ptr [rsp+0E0h]  ; restore R14
    mov         r15,qword ptr [rsp+0E8h]  ; restore R15
    mov         rdi,qword ptr [rsp+0F0h]  ; restore RDI
    mov         rsi,qword ptr [rsp+0F8h]  ; restore RSI
    mov         rbx,qword ptr [rsp+100h]  ; restore RBX
    mov         rbp,qword ptr [rsp+108h]  ; restore RBP

    mov         rax,qword ptr [rsp+110h]  ; restore hidden address of transport_t

    lea         rsp,[rsp+118h] ; prepare stack

; restore return-address
    pop         r10

; transport_t returned in RAX
; return parent fcontext_t
    mov         qword ptr [rax],r9
; return data
    mov         qword ptr [rax+8],r8

; transport_t as 1.arg of context-function
    mov         rcx,rax

; indirect jump to context
    jmp         r10
jump_fcontext ENDP
END